package com.jz.legalpersonclass.service.impl;

import com.jz.legalpersonclass.mapper.LegalPersonMapper;
import com.jz.legalpersonclass.model.*;
import com.jz.legalpersonclass.service.LegalPersonService;
import com.jz.util.AllRecordsVo;
import com.jz.util.ResultBuildVo;
import com.jz.util.ResultVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * Created by WISE on 2021/12/26.
 */
@Service
public class LegalPersonServiceImpl implements LegalPersonService {

    @Autowired
    private LegalPersonMapper legalPersonMapper;

    @Override
    public ResultVo firstPage(LegalPersonSelectVo legalPersonSelectVo) {
        List<LegalPersonVo> legalPersonVoList = legalPersonMapper.queryData(legalPersonSelectVo);
        int count = legalPersonMapper.queryCount(legalPersonSelectVo);
        AllRecordsVo allRecordsVo = new AllRecordsVo();
        allRecordsVo.setDataList(legalPersonVoList);
        allRecordsVo.setTotalSize(count);
        allRecordsVo.setPageIndex(legalPersonSelectVo.getPageIndex());
        allRecordsVo.setPageSize(legalPersonSelectVo.getPageSize());
        allRecordsVo.countPage(count);

        return ResultBuildVo.success(allRecordsVo);
    }

    @Override
    public ResultVo addSubClass(LegalPersonAddVo legalPersonAddVo) {
        LegalPersonVo legalPersonVO = legalPersonMapper.queryByCode(legalPersonAddVo.getLegalPersonCode());
        if (legalPersonVO != null) {
            return ResultBuildVo.error("编码已经存在！");
        }
        //验证名称是否存在
        LegalPersonVo subClassName = legalPersonMapper.queryByName(legalPersonAddVo.getLegalPersonName());
        if (subClassName != null) {
            return ResultBuildVo.error("名称已经存在！");
        }
        Date nowDate = new Date();
        LegalPersonVo classVo = new LegalPersonVo();
        BeanUtils.copyProperties(legalPersonAddVo, classVo);
        classVo.setCreateTime(nowDate);
        classVo.setCreateName("通过Session拿到当前用户名称");
        legalPersonMapper.add(classVo);
        return ResultBuildVo.success();
    }


    @Override
    public ResultVo queryById(Integer id) {

        LegalPersonVo legalPersonVo = legalPersonMapper.queryById(id);

        return ResultBuildVo.success(legalPersonVo);
    }

    @Override
    public ResultVo updateSubClass(@Valid LegalPersonUpdateVo legalPersonUpdateVo) {
        LegalPersonVo legalPersonVo = legalPersonMapper.queryById(legalPersonUpdateVo.getId());
        if (legalPersonVo == null) {
            return ResultBuildVo.error("修改对象不存在");
        }

        if (legalPersonVo.getEnabled()) {
            return ResultBuildVo.error("只有禁用状态才能修改");

        }
        LegalPersonVo subClassName = legalPersonMapper.queryByName(legalPersonUpdateVo.getLegalPersonName());
        if (subClassName != null && legalPersonUpdateVo.getId() != subClassName.getUserId()) {

            return ResultBuildVo.error("修改名称已存在");
        }
        Date nowDate = new Date();
        LegalPersonVo classVo = new LegalPersonVo();
        BeanUtils.copyProperties(legalPersonUpdateVo, classVo);
        classVo.setUpdateTime(nowDate);
        classVo.setUpdateName("通过Session拿到当前用户名称");
        int version = legalPersonMapper.update(classVo);
        if (version == 0) {

            return ResultBuildVo.error("已被修改，刷新重试");
        }
        return ResultBuildVo.success();

    }

    @Override
    public ResultVo updateStatus(@Valid LegalPersonUpdateStatusVo legalPersonUpdateStatusVo) {
        LegalPersonVo legalPersonVo = legalPersonMapper.queryById(legalPersonUpdateStatusVo.getId());
        if (legalPersonVo == null) {
            return ResultBuildVo.error("修改对象不存在");
        }
        Date nowDate = new Date();
        LegalPersonVo classVo = new LegalPersonVo();
        BeanUtils.copyProperties(legalPersonUpdateStatusVo, classVo);
        classVo.setUpdateTime(nowDate);
        classVo.setUpdateName("通过Session拿到当前用户名称");
        legalPersonMapper.updateStatus(classVo);

        return ResultBuildVo.success();

    }

    @Override
    public ResultVo queryDown(Integer userId) {

        List<UserVo> userVoList = legalPersonMapper.queryDown(userId);

        return ResultBuildVo.success(userVoList);
    }


}

